﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Commerce.Proxy.TransactionService.v201001;
using CommerceAccountProxy = Microsoft.Commerce.Proxy.AccountService;
using TestWebApplication.CommerceTestDataProvider;
using TestWebApplication.CommerceTransaction;

namespace TestWebApplication
{
    public class AsyncPurchaseHandler
    {
        public PaymentMethod generatePaymentMethod(string pitype)
        {
            if (pitype.Equals("CobreBemBoleto")) {
                BoletoPaymentMethod boletoPaymentMethod = new BoletoPaymentMethod();
                boletoPaymentMethod.CountryCode = "BR";
                return boletoPaymentMethod;
            }

            else if (pitype.Equals("CobreBemNetBanking"))
            {
                BrazilBankTransferPaymentMethod bankTransferPaymentMethod = new BrazilBankTransferPaymentMethod();
                bankTransferPaymentMethod.CountryCode = "BR";
                bankTransferPaymentMethod.BankName = "Test Bank";
                bankTransferPaymentMethod.PaymentSuccessUrl = "aaa";
                bankTransferPaymentMethod.PaymentFailureUrl = "aaa";
                return bankTransferPaymentMethod;
            }

            else if (pitype.Equals("CobreBemMacadoPago")) {
                MercadoPagoPaymentMethod macadoPagoPaymentMethod = new MercadoPagoPaymentMethod();
                macadoPagoPaymentMethod.CountryCode = "BR";
                macadoPagoPaymentMethod.PaymentSuccessUrl = "aaa";
                macadoPagoPaymentMethod.PaymentFailureUrl = "aaa";
                return macadoPagoPaymentMethod;
            }

            return null;
        }

        public static PurchaseOutput purchase(PaymentMethod paymentMethod, string accountID, CommerceAccountProxy.Identity requester, PurchaseInfoInput purchaseInfo)
        {
            BillingInfo billingInfo = new BillingInfo();
            billingInfo.BillingMode = BillingMode.ImmediateSettle;
   
            billingInfo.PaymentMethod = new PaymentMethod[] { paymentMethod };

            Microsoft.Commerce.Proxy.TransactionService.v201001.CallerInfo callerInfo = CommerceTransactionDataProvider.CreateCallerInfo(requester, accountID);

            Marketplace marketPlace = new Marketplace();
            marketPlace.MarkerplaceGuid = new Guid(Constants.MarketPlaceGuid);
            marketPlace.MarketplaceName = Constants.MarketPlaceName;

            PurchaseContext purchaseContext = new PurchaseContext();
            purchaseContext.ComputeOnly = false;
            purchaseContext.TimeStamp = DateTime.Now;

            CommerceTransactionServiceAdapter transactionAdapter = AdapterFactory.createTransactionServiceAdapter();
            transactionAdapter.Initialize();

            PurchaseOutput purchaseResult = transactionAdapter.Purchase(billingInfo, callerInfo, marketPlace, purchaseContext, purchaseInfo);
            if (purchaseResult.Ack == AckCodeType.Success)
            {
                string purchaseID = purchaseResult.PurchaseInfoOutput.PurchaseId;
                PurchaseInfo info = new PurchaseInfo();
                info.purchaseID = purchaseID;
                info.paymentMothod = paymentMethod;

                PurchaseHandler.purchases.Add(info);
            }

            return purchaseResult;

        }
    }
}